﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace FTUninstaller
{
    public partial class frmUninstaller : Form
    {
        public frmUninstaller()
        {
            InitializeComponent();
        }


        private void cmdExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void SetUIMode(Boolean bEnabled)
        {
            cmdAgreeAndUninstall.Enabled = bEnabled;
            chkCreateRestore.Enabled = bEnabled;
            cmdExit.Enabled = bEnabled;
        }

        private void cmdAgreeAndUninstall_Click(object sender, EventArgs e)
        {
            if (DialogResult.No == MessageBox.Show(this, "Are you sure you want to uninstall QTP? This action is ir-reversibile\n\n", "Confirm Action", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                return;
            SetUIMode(false);
            try
            {
                SysRestore restorePoint = new SysRestore();
                SysRestore.RestorePointInfo oRSPoint = new SysRestore.RestorePointInfo();
                SysRestore.STATEMGRSTATUS oStatus = new SysRestore.STATEMGRSTATUS();

                if (chkCreateRestore.Checked)
                {
                    oRSPoint.dwEventType = (int)SysRestore.BeginSystemChange;
                    oRSPoint.dwRestorePtType = (int)SysRestore.RestoreType.ApplicationUninstall;
                    oRSPoint.szDescription = "QTP Clean Uninstaller by KnowledgeInbox - " + DateTime.Now.ToString();
                    bool bRSPoint = SysRestore.SRSetRestorePointW(ref oRSPoint, out oStatus);
                }

                QTPUninstaller oScanner;
                oScanner = new QTPUninstaller();
                oScanner.UpdateProgress += new UpdateProgressEventHandler(oScanner_UpdateProgress);
                oScanner.CleaningCompleted += new CleaningCompletedEventHandler(oScanner_CleaningCompleted);
                oScanner.UninstallQTP();
                if (chkCreateRestore.Checked)
                {
                    oRSPoint = new SysRestore.RestorePointInfo();
                    oRSPoint.dwEventType = (int)SysRestore.EndSystemChange;
                    oRSPoint.llSequenceNumber = oStatus.llSequenceNumber;
                    SysRestore.SRSetRestorePointW(ref oRSPoint, out oStatus);
                }
            }
            finally { }
            SetUIMode(true);
        }

        void oScanner_CleaningCompleted(object sender, EventArgs e)
        {
            this.lblStatus.Text = "QTP Uninstalled Cleanly";
        }

        void oScanner_UpdateProgress(object sender, UpdateProgressEventArgs e)
        {
            this.lblStatus.Text = (string) e.Item + " " + e.Value.ToString() +"/" + e.MaxValue.ToString();
            if (e.Value % 20 == 0) Application.DoEvents();
        }

        private void lblKnowledgeInbox_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            lblKnowledgeInbox.LinkVisited = true;
            System.Diagnostics.Process.Start("http://knowledgeinbox.com/");  
        }
    }
}
